﻿using lingopen.Application;
using lingopen.Application.System;
using lingopen.Entity.System;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using System;
using System.IO;
using System.Net.Http.Headers;

namespace lingopen.Jabil.Server.Areas.SystemManage.Controllers
{
    /// <summary>
    /// 服务管理
    /// </summary>
    [Area(AreaNames.SystemManage)]
    [LoginAuthorizeAttribute]
    public class ServiceController : WebController
    {

        private IHostingEnvironment _hostingEnvironment;
        /// <summary>
        /// 构造服务管理模块
        /// </summary>
        /// <param name="hostingEnvironment"></param>
        public ServiceController(IHostingEnvironment hostingEnvironment)
        {
            _hostingEnvironment = hostingEnvironment;
        }
        /// <summary>
        /// 返回首页
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="pagination">页码</param>
        /// <param name="keyword">关键字</param>
        /// <returns></returns>

        [HttpGet]
        public ActionResult GetModels(Pagination pagination,string keyword)
        {
            PagedData<Sys_Service> data = this.CreateService<IServiceAppService>().GetServices(pagination,keyword);
            return this.SuccessData(data);
        }

        [HttpPost]
        //[ValidateAntiForgeryToken]
        public ActionResult Add(AddServiceInput input)
        {
            input.CreateUserId = this.CurrentSession.UserId;
            this.CreateService<IEntityAppService>().AddFromDto<Sys_Service, AddServiceInput>(input);
            return this.AddSuccessMsg();
        }
        [HttpPost]
        //[ValidateAntiForgeryToken]
        public ActionResult Update(UpdateServiceInput input)
        {
            this.CreateService<IEntityAppService>().UpdateFromDto<Sys_Service, UpdateServiceInput>(input);
            return this.UpdateSuccessMsg();
        }

        [HttpPost]
        //[ValidateAntiForgeryToken]
        public ActionResult Delete(string id)
        {
            this.CreateService<IEntityAppService>().SoftDelete<Sys_Service>(id, this.CurrentSession.UserId);
            return this.DeleteSuccessMsg();
        }
        [HttpPost]
        public IActionResult Import(string enCode)
        {
            try
            {
                long size = 0;
                var files = Request.Form.Files;
                
                string path = "";
                string filename = "";
                foreach (var file in files)
                {
                    filename = ContentDispositionHeaderValue
                                   .Parse(file.ContentDisposition)
                                   .FileName
                                   .Trim('"');
                    path = _hostingEnvironment.WebRootPath + @"\zips"; 

                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);//创建目录
                    }
                    string fullfilename = path+$@"\{filename}";
                    size += file.Length;
                    using (FileStream fs = System.IO.File.Create(fullfilename))
                    {
                        file.CopyTo(fs);
                        fs.Flush();
                    }
                    //TODO

                }
                bool ret = this.CreateService<IServiceAppService>().UpdateZipPath(enCode, path, filename,size);
                if (ret)
                    return SuccessMsg("zip文件上传成功!");
                else
                    return FailedMsg("zip文件上传失败,请重试!");
            }
            catch (Exception ex)
            {
                return FailedMsg(ex + "");
            }
        }
    }

}